package com.frxuexi.lib_base

import android.app.Application
import android.content.Context
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.base.BaseAppViewModel
import com.frxuexi.lib_base.base.BaseViewModel
import kotlin.properties.Delegates

open class BaseApp :Application(),ViewModelStoreOwner{
    private var vmFactory:ViewModelProvider.Factory?=null

    companion object{
        var appContext:Context by Delegates.notNull<Context>()
        lateinit var baseViewModel: BaseAppViewModel
    }

    override fun onCreate() {
        super.onCreate()


        appContext=applicationContext
        baseViewModel=ViewModelProvider(this,getViewModelFactory()).get(BaseAppViewModel::class.java)

    }

    fun getViewModelFactory():ViewModelProvider.Factory{
        if(vmFactory==null){
            vmFactory=ViewModelProvider.AndroidViewModelFactory.getInstance(this)
        }
        return vmFactory as ViewModelProvider.Factory
    }



    override fun getViewModelStore(): ViewModelStore {
        return ViewModelStore()
    }
}